﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using System.IO;
using University.Workshop.Model;

namespace University.Workshop.WinApp
{
    public partial class NewPatient : Form
    {
        private OpenFileDialog loadImage = new OpenFileDialog();
        private string nameImage = "anonimos.jpg";

        public NewPatient()
        {
            InitializeComponent();
            comboBoxGender.SelectedItem = "Male";
            try
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", nameImage)));
                pictureBoxImage.Image = picture;
            }
            catch (Exception)
            {
                
            }
            IssuePermits();
        }

        private void IssuePermits()
        {
            //buttonSave.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Patient patient = new Patient();
            if (ValidateFields.ValidatingFieldsNewPatient())
            {
                patient.CIPatient = textBoxCI.Text;
                patient.FirstName = textBoxFirstName.Text;
                patient.LastName = textBoxLastName.Text;
                patient.Address = textBoxAddress.Text;
                patient.CellPhone = int.Parse(textBoxCellPhone.Text);
                patient.Phone = int.Parse(textBoxPhone.Text);
                patient.Birthday = DateTime.Today; ;
                patient.Email = textBoxEmail.Text;
                patient.Gender = comboBoxGender.Text;
                patient.ImagePatient = loadImage.SafeFileName;
                                
                if (PatientManager.InsertNewPatient(patient))
                {
                    MessageBox.Show("Successful registration");
                    try
                    {
                        File.Copy(loadImage.FileName, Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", loadImage.SafeFileName)));
                    }
                    catch (Exception)
                    {

                    }
                    this.Close();
                }
                else
                {
                    MessageBox.Show("The user could not be registered");
                }
            }
            else
            {
                MessageBox.Show("All fields are required");
            }
        }        

        private void textBoxCI_Validating(object sender, CancelEventArgs e)
        {
            errorProviderNewPatient.SetError(textBoxCI, ValidateFields.CIValidation(textBoxCI.Text));
        }

        private void textBoxFirstName_Validating(object sender, CancelEventArgs e)
        {
            errorProviderNewPatient.SetError(textBoxFirstName, ValidateFields.FirstNameValidation(textBoxFirstName.Text));
        }

        private void textBoxLastName_Validating(object sender, CancelEventArgs e)
        {
            errorProviderNewPatient.SetError(textBoxLastName, ValidateFields.LastNameValidation(textBoxLastName.Text));
        }

        private void textBoxPhone_Validating(object sender, CancelEventArgs e)
        {
            errorProviderNewPatient.SetError(textBoxPhone, ValidateFields.PhoneValidation(textBoxPhone.Text));
        }

        private void textBoxCellPhone_Validating(object sender, CancelEventArgs e)
        {
            errorProviderNewPatient.SetError(textBoxCellPhone, ValidateFields.CellPhoneValidation(textBoxCellPhone.Text));
        }

        private void textBoxEmail_Validating(object sender, CancelEventArgs e)
        {
            errorProviderNewPatient.SetError(textBoxEmail, ValidateFields.EmailValidation(textBoxEmail.Text));
        }

        private void textBoxAddress_Validating(object sender, CancelEventArgs e)
        {
            errorProviderNewPatient.SetError(textBoxAddress, ValidateFields.AddressValidation(textBoxAddress.Text));
        }

        private void buttonLoad_Click(object sender, EventArgs e)
        {
            if (loadImage.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pictureBoxImage.ImageLocation = loadImage.FileName;
                nameImage = loadImage.SafeFileName;
            }
        }

        private void buttonRevert_Click(object sender, EventArgs e)
        {
            nameImage = "anonimos.jpg";
            try
            {
                Image picture = Image.FromFile(Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", nameImage)));
                pictureBoxImage.Image = picture;                
            }
            catch (Exception)
            {

            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void NewPatient_FormClosed(object sender, FormClosedEventArgs e)
        {
            Singleton.Instance.valid = true;
        }

        
    }
}
